#!/usr/bin/php
<?php
/**
 *
 * YourSpace: yourspace.php
 * A MySpace monitoring tool, to see when your friends were last on.
 * Created February 17, 2008
 * By David Christian Liedle
 * spiderbyte at gmail dot com
 *
 * Released under the terms of the MIT license.
 *
 * This is the CLI version, modified on 2008.02.19
 *
 * EDIT LINES 130, 131, and add more if you like.
 *
 */
$ys_flaregun[0] = time(); // log the execution start-time
class YourSpace {
    
    var $friend_ids; // the array to hold our friend=>id info
    var $myspace_link = "http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid="; // only change if myspace does
    var $pages; // fills up as $this->getPage() is called.
    var $readout; // the final text to display goes in here
    var $commit_method = "return"; // options: return, echo, lock
    var $is_raw = TRUE; // set to false when baked
    
    // INITIALIZE YOURSPACE CLASS:
    function YourSpace($friends=FALSE){
        // initialize
        if($friends){
            // if an initial array was fed in, use it:
            $this->friend_ids = $friends;
        }
    }
    
    // ADD TO FRIENDS ARRAY, IF NEEDED:
    function addFriend($friend_id, $nickname=FALSE){
        if( !$nickname ) $nickname = $friend_id; // nickname is optional - just set to friend id if not given
        $this->friend_ids[$nickname] = $friend_id;
    }
    
    // GET DATA FROM THE WEBSITE
    function getPage($page_id){
        // for external use without associative array
        $this->pages[$page_id] = $this->curlMyspace($page_id);
    }
    
    function getFriendPage($friend){
        // use friend nickname as id, since we have it:
        $this->pages[$friend] = $this->curlMySpace( $this->friend_ids[$friend] );
    }
    
    function getAllFriendPages($friends_array){
        // cycle through every friend we have info for:
        foreach($friends_array as $friend=>$friend_id){
            $this->getFriendPage($friend);
        }
    }
    
    function curlMyspace($friend_id){
        $url = $this->myspace_link.$friend_id;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $page = curl_exec($ch);
        $info_array = curl_getinfo($ch); # For future use... See http://us.php.net/manual/en/function.curl-getinfo.php for info
        curl_close($ch);
        return $page; // returns the whole, unprocessed myspace page
    }
    
    // PROCESS DATA INTO DESIRED DERIVATIVE
    function string_extract($start, $end, $haystack){
        
        // get out part of a string
        
        $small_stack = stristr( $haystack, $start ); // throw away everything before $start
        
        $start_index = strpos($small_stack, $start) + strlen($start); // get index in condensed haystack
        $end_index = strpos($small_stack, $end); // position of $end in condensed haystack
        
        $target_length = $end_index - $start_index; // big number minus small number = length of target
        $tail_end = stristr($small_stack, $end);
        
        return substr( $small_stack, $start_index, $target_length ); // return just the part between our markers
    
    }
    
    function getPageCharLen($page){
        // return the number of characters in the page, and insert commas for thousands    
        return number_format(strlen($page));
    }
    
    function getPageContentsByTag($page, $tag){
        // for the parts we want to get that are wrapped in a tag (e.g. <title></title>)    
        return trim( $this->string_extract("<".$tag.">", "</".$tag.">", $page));
    }
    
    function getPageTitle($page){
        // extract whatever is between <title> and </title> in this page:
        return trim( $this->getPageContentsByTag($page, "title") );
    }
    
    function getLastLogin($page){
        // Extract the date user last logged in. Currently MySpace serves this as M/D(DD?)/YYYY
        return trim( $this->string_extract("Last Login:", "<br>", $page));
    }
    
    // CREATE HUMAN READABLE OUTPUT - CLI FORMAT
    function bake(){
        $this->getAllFriendPages($this->friend_ids);
        foreach( $this->pages as $friend=>$page ){
            $this->readout .= "\n".strtoupper($friend).":\n";
            $this->readout .= "Last login: ".$this->getLastLogin($page)."\n";
            $this->readout .= "Page is ".$this->getPageCharLen($page)." characters long.\n";
            $this->readout .= "Page title is \"".$this->getPageTitle($page)."\"\n";
            $this->readout .= "Page requested: ".$this->myspace_link.$this->friend_ids[$friend]."\n";
        }
        $this->is_raw = FALSE;
    }
    
    function commit(){
        // just echo for now
        echo $this->readout;
    }

} // end of YourSpace class

// setup test array:
$friends['anicknameforyourfriend'] = "12345678"; // Pick a nickname, and insert the friendid
$friends['anotherfriend'] = "12345678"; // Do as many as you like

// instantiate:
$YourSpaceChecker = new YourSpace($friends);
$YourSpaceChecker->bake();

// see how long this all took!
$ys_flaregun[1] = time(); // Yes, we could have skipped using arrays here. But this leaves it open in case more than 2 markers are desired.
$ys_execute_time = $ys_flaregun[1] - $ys_flaregun[0]; // subtract our start time from our end time, and you have the execute time!
$ys_ex = "Execution took ".$ys_execute_time." seconds."; // make it human-readable


// FINAL OUTPUT:
echo "\nYourSpace Output:\n\n";
$YourSpaceChecker->commit();
echo $ys_ex."\n\n";

?>
